package com.isman.placeslist;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.isman.placeslist.apiobject.Session;
import com.isman.placeslist.apiobject.Status.Location;
import com.isman.placeslist.database.PeopleContent;
import com.isman.placeslist.fb.FacebookClient;
import com.isman.placeslist.setting.ProjectPref;
import com.isman.placeslist.util.Util;
import com.isman.placeslist.ws.AsyncRunnerHeadler;
import com.isman.placeslist.ws.ServiceProvider;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class PageNewStatus extends Activity {
	private static final int TO_CHOINE = 0;
	private static final int IMAGE_CHOINE = 1;
	private static final int CAMERA_ACTIVITY = 0;
	private static final int GALLERY_ACTIVITY = 1;
	private static final int SEARCH_ACTIVITY = 2;

	private Location mLocation;
	public static final String LOCATION_INFO = "locationInfo";

	private int PostType = 0;
	public static final String POST_TYPE = "PostType";
	public static final int MYFEED = 0;
	public static final int FRIENDFEED = 1;
	public static final int TOUSERFEED = 2;

	private int PostEvent = 0;
	public static final String POST_EVENT = "Event";
	public static final int NORMAL = 0;
	public static final int CAPTURE = 1;
	public static final int CHECKIN = 2;

	private long PostToID;
	public static final String POST_TO = "PostTo";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_new_status);
		init();
		initData();
	}

	private void init() {
		vTbtnPolicy = (ToggleButton) findViewById(R.id.tbtn_new_policy);
		vtbtnFacebook = (ToggleButton) findViewById(R.id.tbtn_newStatus_fb);
		vBtnChoine = (Button) findViewById(R.id.btn_new_choich);
		vIbtnPicture = (ImageButton) findViewById(R.id.btn_new_picture);
		vIbtnPlace = (ImageButton) findViewById(R.id.btn_new_place);
		viBtnLogo = (ImageButton) findViewById(R.id.ibtn_head_logo);
		viBtnSend = (ImageButton) findViewById(R.id.ibtn_newstatus_send);
		vBtnClaerPeople = (Button) findViewById(R.id.btn_item_people_close);
		vRelResult = (RelativeLayout) findViewById(R.id.layout_new_result);
		vRelPreview = (RelativeLayout) findViewById(R.id.layout_new_preview);
		vImgPriview = (ImageView) findViewById(R.id.img_new_proview);
		vlblPath = (TextView) findViewById(R.id.lbl_new_path);
		vBtnClaerImage = (Button) findViewById(R.id.btn_new_preview_close);
		vActInput = (AutoCompleteTextView) findViewById(R.id.act_newstatus_people);
		vImgProfile = (ImageView) findViewById(R.id.img_item_people_profile);
		vlblName = (TextView) findViewById(R.id.lbl_item_people_name);
		vEdtMessage = (EditText) findViewById(R.id.et_new_message);

		vRelCheckin = (RelativeLayout) findViewById(R.id.layout_new_checkin);
		vtxtName = (TextView) findViewById(R.id.lbl_new_name);
		vtxtPlace = (TextView) findViewById(R.id.lbl_new_place);
		vtxtCheckinCount = (TextView) findViewById(R.id.lbl_new_checkInCount);
		vbtnClaerCheckin = (Button) findViewById(R.id.btn_new_checkin_close);
		vimgLogo = (ImageView) findViewById(R.id.img_new_location_logo);

		viBtnLogo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(PageNewStatus.this, PageMain.class);
				intent.setAction(Intent.ACTION_MAIN);
				startActivity(intent);
			}
		});	
		vtbtnFacebook.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if (arg1) {
					FacebookClient facebookClient = new FacebookClient(
							PageNewStatus.this);
					mFacebook = facebookClient.getFacebook();

					if (ProjectPref.restoreFbSession(mFacebook,
							PageNewStatus.this)) {
						Toast.makeText(PageNewStatus.this,
								"Shared to Facebook : ON", Toast.LENGTH_LONG)
								.show();
					} else {
						Toast.makeText(PageNewStatus.this,
								"You no login facebook", Toast.LENGTH_LONG)
								.show();
						vtbtnFacebook.setChecked(false);
					}
				} else {
					Toast.makeText(PageNewStatus.this,
							"Shared to Facebook : OFF", Toast.LENGTH_LONG)
							.show();
				}
			}
		});
		viBtnSend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (PostEvent) {
				case NORMAL:
				case CAPTURE:
					mPost();
					break;
				case CHECKIN:
					mCheckIn();
					break;
				}
			}
		});
		vIbtnPicture.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(IMAGE_CHOINE);
			}
		});
		vIbtnPlace.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(PageNewStatus.this, PageSearch.class);
				intent.putExtra(PageSearch.FROM, PageSearch.FROM_NEWSTATUS);
				startActivityForResult(intent, SEARCH_ACTIVITY);
			}
		});
		vBtnClaerPeople.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				vActInput.setVisibility(View.VISIBLE);
				vRelResult.setVisibility(View.GONE);
				vBtnClaerPeople.setVisibility(View.GONE);
			}
		});
		vBtnChoine.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(TO_CHOINE);
			}
		});
		vbtnClaerCheckin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mLocation = null;
				vRelCheckin.setVisibility(View.GONE);
			}
		});
		vBtnClaerImage.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mFilePath = "";
				mImageUri = null;
				vRelPreview.setVisibility(View.GONE);
			}
		});
	}

	private void initData() {
		PostType = getIntent().getIntExtra(POST_TYPE, MYFEED);
		PostEvent = getIntent().getIntExtra(POST_EVENT, NORMAL);

		mCheckToType();

		switch (PostEvent) {
		case CAPTURE:
			mLocation = (Location) getIntent().getExtras().getSerializable(
					LOCATION_INFO);
			mCapture();
			break;
		case CHECKIN:
			mLocation = (Location) getIntent().getExtras().getSerializable(
					LOCATION_INFO);
			mCheckIn();
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case GALLERY_ACTIVITY:
				mImageUri = data.getData();
				mFilePath = mGetPath(mImageUri);

				vImgPriview.setImageURI(mImageUri);
				vlblPath.setText(mFilePath);
				vRelPreview.setVisibility(View.VISIBLE);
				Toast.makeText(getApplicationContext(), mFilePath,
						Toast.LENGTH_SHORT).show();
				break;
			case CAMERA_ACTIVITY:
				mFilePath = mGetPath(mImageUri);

				vImgPriview.setImageURI(mImageUri);
				vlblPath.setText(mFilePath);
				vRelPreview.setVisibility(View.VISIBLE);
				Toast.makeText(getApplicationContext(), mFilePath,
						Toast.LENGTH_SHORT).show();
				if (mLocation != null) {
					mCheckIn();
				}
				break;
			case SEARCH_ACTIVITY:
				mLocation = (Location) data.getExtras().getSerializable(
						LOCATION_INFO);
				mCheckIn();
				break;
			}
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder;
		switch (id) {
		case TO_CHOINE:
			final CharSequence[] feedChoine = { "My Feed", "Friend" };
			builder = new AlertDialog.Builder(this);
			builder.setTitle("Update status to.");
			builder.setItems(feedChoine, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int item) {
					PostType = item;
					mCheckToType();
				}
			});
			return builder.create();
		case IMAGE_CHOINE:
			final CharSequence[] imgChoine = { "Choine from Gallery",
					"Capture a Image" };
			builder = new AlertDialog.Builder(this);
			builder.setTitle("Upload image.");
			builder.setItems(imgChoine, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int item) {
					if (item == 0) {
						mGallery();
					} else {
						mCapture();
					}
				}
			});
			return builder.create();
		}
		return null;
	}

	protected void mCheckToType() {
		switch (PostType) {
		case MYFEED:
			mSession = new Session();
			ProjectPref.restoreSession(mSession, this);
			vlblName.setText(mSession.getName());
			vActInput.setVisibility(View.GONE);
			vRelResult.setVisibility(View.VISIBLE);
			vBtnClaerPeople.setVisibility(View.GONE);

			PostToID = mSession.getId();
			break;
		case FRIENDFEED:
			vActInput.setVisibility(View.VISIBLE);
			vRelResult.setVisibility(View.GONE);
			vBtnClaerPeople.setVisibility(View.GONE);

			// if (mPeopleAdapter == null) {
			// ContentResolver mContentResolver = getContentResolver();
			// Cursor cursor = mContentResolver.query(
			// PeopleContent.CONTENT_URI, null, null, null,
			// PeopleContent.UNAME + " ASC");
			// vActInput.setAdapter(new PeopleAdapter(this, cursor));
			// }
			// vActInput.setText("");
			break;
		case TOUSERFEED:
			vActInput.setVisibility(View.GONE);
			vRelResult.setVisibility(View.VISIBLE);
			vBtnClaerPeople.setVisibility(View.GONE);

			PostToID = getIntent().getExtras().getLong(POST_TO);
			ContentResolver mContentResolver = getContentResolver();
			Cursor cursor = mContentResolver.query(PeopleContent.CONTENT_URI,
					null, PeopleContent.UID + " = ?",
					new String[] { String.valueOf(PostToID) }, null);
			cursor.moveToLast();
			byte[] data = cursor.getBlob(cursor
					.getColumnIndex(PeopleContent.UPICTURE));
			Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
			String name = cursor.getString(cursor
					.getColumnIndex(PeopleContent.UNAME));

			vImgProfile.setImageBitmap(bitmap);
			vlblName.setText(name);
			break;
		}
	}

	protected void mCapture() {
		String fileName = "temp.jpg";
		ContentValues values = new ContentValues();
		values.put(MediaColumns.TITLE, fileName);
		mImageUri = getContentResolver().insert(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
		startActivityForResult(intent, CAMERA_ACTIVITY);
	}

	protected void mGallery() {
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(Intent.createChooser(intent, "Select Picture"),
				GALLERY_ACTIVITY);
	}

	protected void mSearch() {
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(Intent.createChooser(intent, "Select Picture"),
				SEARCH_ACTIVITY);
	}

	protected String mGetPath(Uri uri) {
		String[] projection = { MediaColumns.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

	/*protected void mLikeLocation(Location location) {
		ServiceProvider provider = new ServiceProvider(this);
		provider.LikeLocation(location.getId(), new AsyncRunnerHeadler() {
			@Override
			public void onError(int statusCode, String json) {
				vtxtLikeCount.setText("error");
			}

			@Override
			public void onConnectingError(String message) {
				vtxtLikeCount.setText("error");
			}

			@Override
			public void onComplete(String json) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mLocation.setCanLike(false);
						vtxtLikeCount.setText(Integer.parseInt(vtxtLikeCount
								.getText().toString()) + 1);
						vtbtnLike.setEnabled(true);
					}
				});
			}

			@Override
			public void onAuthenticate(String json) {
				vtxtLikeCount.setText("error");
			}
		});
	}*/

//	protected void mUnlikeLocation(Location location) {
//		ServiceProvider provider = new ServiceProvider(this);
//		provider.UnlikeLocation(location.getId(), new AsyncRunnerHeadler() {
//
//			@Override
//			public void onError(int statusCode, String json) {
//				vtxtLikeCount.setText("error");
//			}
//
//			@Override
//			public void onConnectingError(String message) {
//				vtxtLikeCount.setText("error");
//			}
//
//			@Override
//			public void onComplete(String json) {
//				runOnUiThread(new Runnable() {
//					@Override
//					public void run() {
//						mLocation.setCanLike(true);
//						vtxtLikeCount.setText(Integer.parseInt(vtxtLikeCount
//								.getText().toString()) - 1);
//						vtbtnLike.setEnabled(true);
//					}
//				});
//			}
//
//			@Override
//			public void onAuthenticate(String json) {
//				vtxtLikeCount.setText("error");
//			}
//		});
//	}

	protected void mCheckIn() {
		StringBuilder sb = new StringBuilder();
		if (mLocation.getMap().getSubLocality() != null) {
			sb.append(mLocation.getMap().getSubLocality() + " ");
		}
		if (mLocation.getMap().getLocality() != null) {
			sb.append(mLocation.getMap().getLocality() + " ");
		}
		if (mLocation.getMap().getAdminArea() != null) {
			sb.append(mLocation.getMap().getAdminArea() + " ");
		}
		if (mLocation.getMap().getCountryName() != null) {
			sb.append("\n" + mLocation.getMap().getCountryName() + " ");
		}
		if (mLocation.getMap().getCountryCode() != null) {
			sb.append(mLocation.getMap().getCountryCode());
		}

		vRelCheckin.setVisibility(View.VISIBLE);
		vtxtName.setText(mLocation.getName());
		vtxtPlace.setText(sb);
		vtxtCheckinCount.setText(mLocation.getInfo().getCheckInCount() + "");
		vimgLogo.setImageBitmap(Util.getBitmap(mLocation.getPicture()));
//		vtbtnLike.setChecked(!mLocation.isCanLike());
//		vtbtnLike.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//			@Override
//			public void onCheckedChanged(CompoundButton buttonView,
//					boolean isChecked) {
//				if (isChecked) {
//					vtbtnLike.setEnabled(false);
//					mLikeLocation(mLocation);
//				} else {
//					vtbtnLike.setEnabled(false);
//					mUnlikeLocation(mLocation);
//				}
//			}
//		});
	}

	protected void mPostFbPicture(String message) {
		FacebookClient facebookClient = new FacebookClient(this);
		mFacebook = facebookClient.getFacebook();
		AsyncFacebookRunner runner = new AsyncFacebookRunner(mFacebook);

		Bitmap bmp = BitmapFactory.decodeFile(mFilePath);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);

		Bundle params = new Bundle();
		params.putString("message", message);
		params.putByteArray("picture", baos.toByteArray());

		mDialog = ProgressDialog.show(this, "", "Please wait...");
		runner.request("me/photos", params, "POST", new MyRequestListener(),
				null);
	}

	protected void mPostFb(String message) {
		FacebookClient facebookClient = new FacebookClient(this);
		mFacebook = facebookClient.getFacebook();
		AsyncFacebookRunner runner = new AsyncFacebookRunner(mFacebook);

		Bundle bundle = new Bundle();
		bundle.putString("message", message);

		runner.request("me/feed", bundle, "POST", new MyRequestListener(), null);
	}

	protected void mPostFbLink() {

	}

	protected void mPost() {
		String message = vEdtMessage.getText().toString().trim();
		int policy = vTbtnPolicy.isChecked() == true ? 1 : 0;

		String toCur = "0";
		if (mCurrent != null) {
			toCur = mCurrent.getId() + "";
		}

		if (mFilePath.equals("")) {
			mPostPL(toCur, message, policy);
		} else {
			mPostPLPicture(toCur, message, policy);
		}
	}

	protected void mPostPLPicture(String toCur, final String message, int policy) {
		ServiceProvider provider = new ServiceProvider(this);
		provider.CreateStatus(message, PostToID + "", toCur, mFilePath, policy,
				new ProgressDialog(this), new AsyncRunnerHeadler() {
					@Override
					public void onComplete(String json) {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								if (vtbtnFacebook.isChecked()) {
									mPostFbPicture(message);
								} else {
									Toast.makeText(PageNewStatus.this,
											"Update Complate.",
											Toast.LENGTH_LONG).show();
									Intent intent = new Intent(
											PageNewStatus.this, PageMain.class);
									startActivity(intent);
								}
							}
						});
					}

					@Override
					public void onAuthenticate(String json) {
						mDialog.dismiss();
					}

					@Override
					public void onError(int statusCode, String json) {
						mDialog.dismiss();
					}

					@Override
					public void onConnectingError(String message) {
						mDialog.dismiss();
					}
				});
	}

	protected void mPostPL(String toCur, final String message, int policy) {
		mDialog = ProgressDialog.show(this, "", "Please wait...");
		ServiceProvider provider = new ServiceProvider(this);
		provider.CreateStatus(message, PostToID + "", toCur, mFilePath, policy,
				null, new AsyncRunnerHeadler() {
					@Override
					public void onComplete(String json) {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								if (vtbtnFacebook.isChecked()) {
									mPostFb(message);
								} else {
									mDialog.dismiss();
									Toast.makeText(PageNewStatus.this,
											"Update Complate.",
											Toast.LENGTH_LONG).show();
									Intent intent = new Intent(
											PageNewStatus.this, PageMain.class);
									startActivity(intent);
								}
							}
						});
					}

					@Override
					public void onAuthenticate(String json) {
						mDialog.dismiss();
					}

					@Override
					public void onError(int statusCode, String json) {
						mDialog.dismiss();
					}

					@Override
					public void onConnectingError(String message) {
						mDialog.dismiss();
					}
				});
	}

	private Location mCurrent;
	private String mFilePath = "";
	private Session mSession;
	private Facebook mFacebook;
	private Uri mImageUri;

	private TextView vlblName;
	private TextView vlblPath;
	private ToggleButton vtbtnFacebook;
	private ImageView vImgPriview;
	private ImageView vImgProfile;
	private ImageButton viBtnLogo;
	private ImageButton viBtnSend;
	private Button vBtnClaerImage;
	private Button vBtnClaerPeople;
	private ImageButton vIbtnPicture;
	private ImageButton vIbtnPlace;
	private Button vBtnChoine;
	private ToggleButton vTbtnPolicy;
	private RelativeLayout vRelPreview;
	private RelativeLayout vRelResult;
	private AutoCompleteTextView vActInput;
	private EditText vEdtMessage;
	private ProgressDialog mDialog;

	private RelativeLayout vRelCheckin;
	private ImageView vimgLogo;
	private TextView vtxtName;
	private TextView vtxtPlace;
	private TextView vtxtCheckinCount;
	private Button vbtnClaerCheckin;

	public class MyRequestListener implements RequestListener {
		@Override
		public void onComplete(String response, Object state) {
			Log.e("Facebook", response);
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					mDialog.dismiss();
					Toast.makeText(PageNewStatus.this,
							"Update to facebook complate", Toast.LENGTH_LONG)
							.show();
					Intent intent = new Intent(PageNewStatus.this,
							PageMain.class);
					startActivity(intent);
				}
			});
		}

		@Override
		public void onIOException(final IOException e, Object state) {
			Log.e("Facebook", e.toString());
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					mDialog.dismiss();
					Toast.makeText(PageNewStatus.this, e.toString(),
							Toast.LENGTH_LONG).show();
					Intent intent = new Intent(PageNewStatus.this,
							PageMain.class);
					startActivity(intent);
				}
			});
		}

		@Override
		public void onFileNotFoundException(final FileNotFoundException e,
				Object state) {
			Log.e("Facebook", e.toString());
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					mDialog.dismiss();
					Toast.makeText(PageNewStatus.this, e.toString(),
							Toast.LENGTH_LONG).show();
					Intent intent = new Intent(PageNewStatus.this,
							PageMain.class);
					startActivity(intent);
				}
			});
		}

		@Override
		public void onMalformedURLException(final MalformedURLException e,
				Object state) {
			Log.e("Facebook", e.toString());
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					mDialog.dismiss();
					Toast.makeText(PageNewStatus.this, e.toString(),
							Toast.LENGTH_LONG).show();
					Intent intent = new Intent(PageNewStatus.this,
							PageMain.class);
					startActivity(intent);
				}
			});
		}

		@Override
		public void onFacebookError(final FacebookError e, Object state) {
			Log.e("Facebook", e.toString());
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					mDialog.dismiss();
					Toast.makeText(PageNewStatus.this, e.toString(),
							Toast.LENGTH_LONG).show();
					Intent intent = new Intent(PageNewStatus.this,
							PageMain.class);
					startActivity(intent);
				}
			});
		}
	}
}

// public class PeopleAdapter extends CursorAdapter {
// public PeopleAdapter(Context context, Cursor c) {
// super(context, c);
// mColUID = c.getColumnIndexOrThrow(PeopleContent.UID);
// mColUName = c.getColumnIndexOrThrow(PeopleContent.UNAME);
// mColUPicture = c.getColumnIndexOrThrow(PeopleContent.UPICTURE);
// mInflater = LayoutInflater.from(context);
// }
//
// @Override
// public void bindView(View view, Context context, Cursor cursor) {
// String name = cursor.getString(mColUName);
// byte[] data = cursor.getBlob(mColUPicture);
// Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
//
// TextView vlblLabel = (TextView) view
// .findViewById(R.id.lbl_item_people_name);
// ImageView vimgProfile = (ImageView) view
// .findViewById(R.id.img_item_people_profile);
//
// vlblLabel.setText(name);
// vimgProfile.setImageBitmap(bmp);
// }
//
// @Override
// public View newView(Context context, Cursor cursor, ViewGroup parent) {
// final String name = cursor.getString(mColUName);
// final long userId = cursor.getLong(mColUID);
// byte[] data = cursor.getBlob(mColUPicture);
// final Bitmap bmp = BitmapFactory.decodeByteArray(data, 0,
// data.length);
//
// View view = mInflater.inflate(R.layout.inc_item_people, null);
// TextView _lblLabel = (TextView) view
// .findViewById(R.id.lbl_item_people_name);
// ImageView _imgProfile = (ImageView) view
// .findViewById(R.id.img_item_people_profile);
//
// _imgProfile.setImageBitmap(bmp);
// _lblLabel.setText(name);
//
// view.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// PostToID = userId;
//
// vlblName.setText(name);
// vImgProfile.setImageBitmap(bmp);
// vActInput.setVisibility(View.GONE);
// vRelResult.setVisibility(View.VISIBLE);
// vBtnClaerPeople.setVisibility(View.VISIBLE);
// }
// });
// return view;
// }
//
// @Override
// public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
// if (getFilterQueryProvider() != null) {
// return getFilterQueryProvider().runQuery(constraint);
// }
// String args = null;
// if (constraint != null) {
// args = PeopleContent.UNAME + " LIKE '%" + constraint.toString()
// + "%'";
// } else {
// args = "";
// }
// ContentResolver cr = getContentResolver();
// return cr.query(PeopleContent.CONTENT_URI, null, args, null,
// PeopleContent.UNAME + " ASC");
// }
//
// @Override
// public CharSequence convertToString(Cursor cursor) {
// int columnIndex = cursor.getColumnIndexOrThrow(PeopleContent.UNAME);
// return cursor.getString(columnIndex);
// }
//
// @Override
// protected void init(Context context, Cursor c, boolean autoRequery) {
// super.init(context, c, autoRequery);
// }
//
// @Override
// protected void onContentChanged() {
// super.onContentChanged();
// }
//
// private LayoutInflater mInflater;
// private int mColUName;
// private int mColUID;
// private int mColUPicture;
// }
